package cn.stylefeng.guns.modular.rest;


import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.car.entity.Car;
import cn.stylefeng.guns.modular.car.model.params.CarParam;
import cn.stylefeng.guns.modular.car.service.CarService;
import cn.stylefeng.guns.modular.school.entity.ParkingInfo;
import cn.stylefeng.guns.modular.school.service.ParkingInfoService;
import cn.stylefeng.guns.modular.school.service.ParkingPlaceInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.stylefeng.guns.modular.school.model.params.ParkingPlaceInfoParam;
import cn.stylefeng.roses.core.reqres.response.ResponseData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 绑定控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/gunsApi/car")
public class ApiNeedTokenCarController extends BaseController {

    @Autowired
    private CarService carService;
    @Autowired
    private ParkingInfoService parkingInfoService;
    @Autowired
    private ParkingPlaceInfoService parkingPlaceInfoService;
    /**
     * 绑定
     */
    @PostMapping(value = "add")
    public Object add(CarParam carParam) {
        Map map = new HashMap();

        List<Car> list = carService.list(new QueryWrapper<Car>().eq("open_id", carParam.getOpenId()));
        list.forEach(car -> {
            car.setSelectDefault("0");
            carService.updateById(car);
        });

        carParam.setSelectDefault("1");
        carService.add(carParam);

        List<Car> cars = carService.list(new QueryWrapper<Car>().eq("open_id", carParam.getOpenId()));
        map.put("cars", cars);

        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    @PostMapping(value = "delete")
    public Object delete(CarParam carParam) {
        Map map = new HashMap();

//        List<Car> list = carService.list(new QueryWrapper<Car>().eq("open_id", carParam.getOpenId()));
//        list.forEach(car -> {
//            car.setSelectDefault("0");
//            carService.updateById(car);
//        });
//
//        carParam.setSelectDefault("1");
        carService.delete(carParam);

        List<Car> cars = carService.list(new QueryWrapper<Car>().eq("open_id", carParam.getOpenId()));
        map.put("cars", cars);

        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    @PostMapping(value = "update")
    public Object update(CarParam carParam) {
        Map map = new HashMap();

        List<Car> list = carService.list(new QueryWrapper<Car>().eq("open_id", carParam.getOpenId()));
        list.forEach(car -> {
            car.setSelectDefault("0");
            carService.updateById(car);
        });

        carParam.setSelectDefault("1");
        carService.update(carParam);

        List<Car> cars = carService.list(new QueryWrapper<Car>().eq("open_id", carParam.getOpenId()));
        map.put("cars", cars);

        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    @GetMapping(value = "list")
    public Object list(CarParam carParam) {
        Map map = new HashMap();

        List<Car> cars = carService.list(new QueryWrapper<Car>().eq("open_id", carParam.getOpenId()));
        map.put("cars", cars);

        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }
    @GetMapping(value = "parkinglist")
    @ResponseBody
    public List<ParkingInfo> wxlist() {
        List<ParkingInfo> parkingInfos = parkingInfoService.list();
        return parkingInfos;
    }

    @GetMapping(value = "/addPark")
    @ResponseBody
    public ResponseData addItem(ParkingPlaceInfoParam parkingPlaceInfoParam) {
        this.parkingPlaceInfoService.add(parkingPlaceInfoParam);
        return ResponseData.success();
    }

    @ResponseBody
    @RequestMapping("/myParkList")
    public LayuiPageInfo list(ParkingPlaceInfoParam parkingPlaceInfoParam) {
        return this.parkingPlaceInfoService.findPageBySpec(parkingPlaceInfoParam);
    }

    @RequestMapping("/deletePark")
    @ResponseBody
    public ResponseData delete(ParkingPlaceInfoParam parkingPlaceInfoParam) {
        this.parkingPlaceInfoService.delete(parkingPlaceInfoParam);
        return ResponseData.success();
    }

}


